package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.MeetingApplyAddDTO;
import com.oa.admin.dto.MeetingApplyListDTO;
import com.oa.admin.entity.MeetingApply;
import com.oa.admin.service.IMeetingApplyService;
import com.oa.admin.vo.MeetingApplyDetailsVO;
import com.oa.admin.vo.MeetingApplyListVO;
import com.oa.core.PageResult;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/meetingApply")
@Api(description = "会议活动申请表模块")
public class MeetingApplyController extends BaseController {

    private final IMeetingApplyService meetingApplyService;

    @Autowired
    public MeetingApplyController(IMeetingApplyService meetingApplyService) {
        this.meetingApplyService = meetingApplyService;
    }


    @GetMapping("/list")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<PageResult<MeetingApplyListVO>> list(@ApiParam("查询对象") MeetingApplyListDTO meetingApplyListDTO) {
        meetingApplyListDTO.setUserId(getLoginUserId());
        return new Resp().success(meetingApplyService.findMeetingApplyList(meetingApplyListDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<MeetingApplyDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(meetingApplyService.findMeetingApplyById(id));
    }


    /**
     * 新增
     *
     * @param meetingApplyAddDTO MeetingApplyAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "会议活动申请表对象") @RequestBody(required = false) MeetingApplyAddDTO meetingApplyAddDTO) throws BusinessException, DataIsEmptyException {
        meetingApplyAddDTO.setProposer(getLoginUserId());
        return meetingApplyService.add(meetingApplyAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param meetingApply MeetingApply
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "会议活动申请表对象") @RequestBody(required = false) MeetingApply meetingApply) {
        if (meetingApplyService.updateById(meetingApply)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        MeetingApply meetingApply = meetingApplyService.selectById(id);
        if (meetingApply != null && meetingApply.getIsDeleted() == 2) {
            meetingApply.setIsDeleted(1);
            if (meetingApplyService.updateById(meetingApply)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
